# -*- coding: utf-8 -*-
"""
Created on Sun Nov 11 20:08:19 2018

@author: Sz-wyz
"""

#-*- coding: utf-8 -*-
import pandas as pd
from random import shuffle
import os
import numpy
import string 
import random

df1 = pd.read_csv(r'file1.csv',encoding = 'gbk')
df1 = df1.drop(70679,axis=0)#脏数据所在70679

data_a = df1[df1['地点']=='A']
data_b = df1[df1['地点']== 'B']
data_c = df1[df1['地点'] == 'C']
data_d = df1[df1['地点'] == 'D']
data_e = df1[df1['地点'] == 'E']
#
data_a.to_csv('task1-1A.csv')
data_b.to_csv('task1-1B.csv')
data_c.to_csv('task1-1C.csv')
data_d.to_csv('task1-1D.csv')
data_e.to_csv('task1-1E.csv')
print("每台售货机销售信息保存完成")
def Func(data_x, s):
    data_x = data_x[['地点','mounth','大类','实际金额']]
    
def Func(data_x, s):
    data_x = data_x[['地点','mounth','大类','实际金额']]
    def Group(data):
        totals = data.groupby('大类')['实际金额'].sum()
        return totals
    f = lambda x: Group(x)
    grouped = data_x.groupby('mounth')
    data_x = grouped.apply(Group)
    
    data_x.to_csv('tempfile.csv')
    data_x = pd.read_csv('tempfile.csv', encoding = 'gbk')
    data_x['地点'] = s
    return data_x
data_a = df1[df1['地点']=='A']
data_a = Func(data_a, 'A')

data_b = df1[df1['地点']=='B']
data_b = Func(data_b, 'B')

data_c = df1[df1['地点']=='C']
data_c = Func(data_c, 'C')

data_d = df1[df1['地点']=='D']
data_d = Func(data_d, 'D')

data_e = df1[df1['地点']=='E']
data_e = Func(data_e, 'E')

Data = pd.concat([data_a,data_b,data_c,data_d,data_e])
data_x = Data.drop(['0','1'],axis = 1)
label_0 = Data['0']
label_1 = Data['1']
data_x = pd.get_dummies(data_x)


'''
预测
'''
from sklearn import datasets
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

data_x = data_x.values
label_0 = label_0.values
label_1 = label_1.values

train_x,test_x,train_y,test_y = train_test_split(data_x,
                                            label_0,
                                             test_size = 0.3)
model = LinearRegression()
model.fit(data_x,label_0)

model.score(test_x,test_y)

model.predict(np.array([1,1,0,0,0,0]).reshape(1,-1))
import matplotlib.pyplot as plt
clf = LinearRegression()
clf.fit(train_x, train_y)

print(clf.score(train_x, train_y))
predict = clf.predict(train_x)
plt.scatter(predict, train_y, s=2)
predict_y = clf.predict(train_x[4].reshape(1,-1))
print("predict: %.2f, actually: %.2f" % (predict_y, train_y[4]))
plt.plot(predict_y, predict_y, 'ro')
plt.plot([train_y.min(), train_y.max()], [train_y.min(), train_y.max()], 'k--', lw=2)
predict_test = clf.predict(test_x)
print(clf.score(test_x,test_y))
plt.scatter(predict_test, test_y, s=2)
plt.plot([test_y.min(), test_y.max()], [test_y.min(), test_y.max()], 'k--', lw=2)
model.predict(np.array([1,1,0,0,0,0]).reshape(1,-1))
